package cn.agiledata.bank.webService.ser;

/*
 * Copyright (c) 2003-2005 Lan-in corporation.  All rights reserved.
 *
 */
 
//import here
import org.apache.axis.Constants;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.SimpleValueSerializer;
import org.apache.axis.wsdl.fromJava.Types;

import org.w3c.dom.Element;
import org.xml.sax.Attributes;

import javax.xml.namespace.QName;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  Serializer for xsd:dateTime type data, convert java.util.Date
 *  type object to dateTime string, the destination string contains
 *  no TimeZone information, just year, month, day, hour, minute,
 *  second, millisecond.
 *  
 */
public class DateTimeSerializer implements SimpleValueSerializer
{
    private static SimpleDateFormat fmt =
       new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

    /**
     * Serialize a Date.
     */
    public void serialize(QName name, Attributes attributes,
                          Object value, SerializationContext context)
        throws IOException
    {
        context.startElement(name, attributes);
        context.writeString(getValueAsString(value, context));
        context.endElement();
    }

    public String getValueAsString(Object value, SerializationContext context)
    {
        Date date = (Date)value;

        synchronized (fmt)
        {
            // Sun JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4229798.html
            return fmt.format(date);
        }
    }

    public String getMechanismType()
    {
        return Constants.AXIS_SAX;
    }

    /**
     * Return XML schema for the specified type, suitable for insertion into
     * the &lt;types&gt; element of a WSDL document, or underneath an
     * &lt;element&gt; or &lt;attribute&gt; declaration.
     *
     * @param javaType the Java Class we're writing out schema for
     * @param types the Java2WSDL Types object which holds the context
     *              for the WSDL being generated.
     * @return a type element containing a schema simpleType/complexType
     * @see org.apache.axis.wsdl.fromJava.Types
     */
    public Element writeSchema(Class javaType, Types types)
    throws Exception
    {
        return null;
    }
}